<?php
/**
 *  This class is the base for file replacements, supporting an inplace replacement feature
 * 
 *
 *
 */
 
 
 /**
 *
 *
 *
 */
 
 /**
 *
 *
 *
 */
class MakePage {
/**
 *
 * Holds pairs of ar_repsets[<token>]=<replace text>
 * 
 *
 */
protected $ar_repsets;

protected $tag = '%';

protected $endtag = 'END';

/**
 *
 * Holds replacement context information for each context in which we can do replacements
 * There will usually be 3 of these for most php files:  php, html, and js
 * The replacer (ReplaceInPlace) will run for each context defined in the array, and look for
 * replacements using the ar_repsets array for token - values pairs
 * ['startcmt'] = start comment string
 * ['endcmt'] = end comment string
 *
 */
protected $ar_repcontexts;

/**
 *
 * Holds data sets, used in creating replacements
 *
 */
public $ar_data;

public $str_log;

public function __construct(){


	// initialze 
	$this->tag = '%';
	$this->entdag = 'END';

	// setup default replacement contexts
	$this->ar_repcontexts['php']['startcmt']='/*';
	$this->ar_repcontexts['php']['endcmt']= '*/';
	$this->ar_repcontexts['html']['startcmt']='<!--';
	$this->ar_repcontexts['html']['endcmt']='-->';
	//$this->ar_repcontexts['js']['startcmt']='/*';
	//$this->ar_repcontexts['js']['endcmt']='*/';
}

public function AddReplacement($token,$value){
	$this->ar_repsets[$token]=$value;
}

public function PrintReplacementsTable(){
	echo '<table>';
	foreach($this->ar_repsets AS $token=>$value){
		echo "<tr><td>$token</td><td>$value</td></tr>";
	}
	echo '<table>';
}


/**
 * ReplaceInFile($tplfile,$targetfile)
 *
 *
 *
 */
public function ReplaceInFile($tplfile,$targetfile){
	// if the file exists, use the target file as it's own template (replace in place)
	if(file_exists($targetfile)){
		$tplfile = $targetfile;
	}
	$str_file = file_get_contents($tplfile);
	foreach($this->ar_repcontexts AS $ctype => $context){
		$str_file = $this->ReplaceInPlace($str_file,$this->tag,$context['startcmt'],$context['endcmt'],$this->endtag);
	}
	//$str_file = $this->ReplaceInPlace($str_file,"%","/*","*/","END");
	
	//file_put_contents($targetfile,$str_file);	
	return $str_file;

}



	/**
	* ReplaceInPlace($str_start_file,$tag,$startcmt,$endcmt,$endtag = 'END')
	* This functions executes a set of inplace replacements on a string ($str_start_file)
	* @param $str_start_file tring of input file text
	* @param $tag:  tag to use for finding tokens
	* @param $startcmt:  string to start comment for tokens in set
	*/	
	public function ReplaceInPlace($str_start_file,$tag,$startcmt,$endcmt,$endtag = 'END'){
	
		$startcmt_chars = strlen($startcmt);
		$endcmt_chars = strlen($endcmt);
		$tag_chars = strlen($tag);
		$endtag_chars = strlen($endtag);
		//$str_start_file = file_get_contents($filename);
		//echo 'ALL:' . $str_start_file;
//	echo 'ALL:' . $str_start_file;		
	if(strpos($str_start_file,$startcmt . $tag)){

		while(strpos($str_start_file,$startcmt . $tag)){
			// position in file of the beggining of the start segment (segment = comment + tag + opcode)
			$start_pos = strpos($str_start_file,$startcmt . $tag);
			// sring between the beggining of the file and the start segment
			$str_before = substr($str_start_file,0,$start_pos);
			// string between the beggining of the start ta and the end of the file
			$str_after = substr($str_start_file,$start_pos);
			//position, relative to text after the sart segment, of the end of the end segment
			$end_pos = strpos($str_after, $endtag ) + $tag_chars + $endtag_chars + $endcmt_chars;
			// The string after the end (and start) segments, and after the stuff to be inserted
			$str_post = substr($str_after,$end_pos);		
			// The position of tag at the end of the opcode in the start
			$opcode_end_pos = strpos($str_after,$tag,$tag_chars + $startcmt_chars);
			// The opcode from the start segment
			$str_opcode = substr($str_after,$tag_chars + $startcmt_chars,$opcode_end_pos - $tag_chars - $startcmt_chars - $tagchars);
			
			$this->str_log .= 'FOUND TOKEN:' . $str_opcode . "\n";
			
			if(!empty($this->ar_repsets[$str_opcode])){
				$data = $this->ar_repsets[$str_opcode];
			$this->str_log .= "\n<br />FOUND REPLACEMENT FOR $str_opcode :" . $data . "\n";				
			}else{
			$this->str_log .= "\n<br />FOUND NO REPLACEMENT FOR $str_opcode " . "\n";			
				$data = '';
			}
//			echo 'ALL:' . $str_start_file;
			$str_new .= $str_before . $startcmt . $tag . $str_opcode . $tag .  $endcmt . ' '. $data .' '. $startcmt . $endtag . $tag . $endcmt ;
			$this->str_log .= "REPLACE WITH: $str_opcode :" . $str_before . $startcmt . $tag . $str_opcode . $tag .  $endcmt . ' '. $data .' '. $startcmt . $endtag . $tag . $endcmt ;			
			$this->str_log .= "newfile : \n" . $str_new . "\n";			
					
			//echo '<pre>';
			//echo '<br />STR_BEFORE:' . $str_before;
			//echo '<br />START POS:' . $start_pos;
			//echo '<br />STR_AFTER:' . $str_after;
			//echo '<br />STR_POST:' . $str_post;
			//echo '<br/ >END_POS:' . $end_pos;
			//echo '<br />OPCODE_END_POS:' . $opcode_end_pos;
			//echo '<br />STR_OPCODE:' . $str_opcode;
			//echo '<br />STR_NEW:' . $str_new;
			//echo '<br />-------------------------------------------------<br /></pre>';
			$str_start_file = $str_post;
			}
		$str_new .= $str_post;
		//return($str_new);
	}else{
		echo "<p>Found no tokens in context startcomment: $startcmt endcomment: $endcmt tag: $tag op_code: $str_opcode </p>\n";
		$str_new = $str_start_file;
	}
//			echo '<br />STR_OUT:' . $str_new;
			return($str_new);
	}

// function that replaces from a template, always overwriting the file 
	public function ReplaceFromTemplate($str_file,$tag){
		//$str_file = file_get_contents($tpl_file);
		foreach($this->ar_replacements AS $token=>$ar_repset){
			    $str_list_file = str_replace($tag . $token . $tag, $ar_repset['value'], $str_file);
		}
		return $str_list_file;
	}	
	

	
	
}



?>
